package poplar.dc.test;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.Reader;
import java.util.Hashtable;
import java.sql.*;
import javax.xml.bind.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import poplar.dc.user.*;

public class testMybatis {	
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}	
	
	@Test
	public void testUserInsert(){
		String resource = "mybatis.xml";
		Reader reader;
		User user = new User();
		user.setStrID("apov100");
		user.setType((short)3);
		user.setEmail("apov@naver.com");
		
		try{
			reader = Resources.getResourceAsReader( resource );
			SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build( reader );
			
			SqlSession session = sqlMapper.openSession();
			try{
				session.insert( "UserMapper.insert_user", user );				
			} 
			catch( Exception e ){
				System.out.println( e.getMessage() );
			}
			finally{
				session.commit();
				session.close();
			}
		} 
		catch( IOException e ){
			e.printStackTrace();
		}		
	}
	
	@Test
	public void testUserLoad(){
		String resource = "mybatis.xml";
		Reader reader;
		User user = null;		
		
		try{
			reader = Resources.getResourceAsReader( resource );
			SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build( reader );
			
			SqlSession session = sqlMapper.openSession();
			try{
				user = ( User )session.selectOne( "UserMapper.select_user", "apov" );				
			} 
			catch( Exception e ){
				System.out.println( e.getMessage() );
			}
			finally{
				session.close();
			}
		} 
		catch( IOException e ){
			e.printStackTrace();
		}
		
		System.out.println( user.getUID() );
		System.out.println( user.getRegDate() );
		System.out.println( user.getType() );
	}
	
	@Test
	public void testUserBean(){
		String resource = "mybatis.xml";
		Reader reader;
		long uid = 0;		
		
		try{
			reader = Resources.getResourceAsReader( resource );
			SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build( reader );
			
			SqlSession session = sqlMapper.openSession();
			try{
				Object o = session.selectOne( "UserMapper.select_uid", "apov" );
				uid = Long.parseLong( o.toString() );				
			} 
			catch( Exception e ){
				System.out.println( e.getMessage() );
			}
			finally{				
				session.close();
			}
		} 
		catch( IOException e ){
			e.printStackTrace();
		}
		
		System.out.println( uid );		
	}
}
